from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser
#用户表
class User(AbstractUser):
    #继承原来的auth表，拓展字段，不要与原来有的字段名重复
    telephone = models.CharField(max_length=11,verbose_name='手机号码')
    icon = models.ImageField(upload_to='icon',default='/icon/default.jpg/',verbose_name='用户头像')
    name = models.CharField(max_length=12,verbose_name='用户姓名')
    sid = models.CharField(max_length=24,verbose_name='身份证')
    sex = models.IntegerField(choices=((1,'男'),(0,'女')),default=1,verbose_name='性别')
    role = models.IntegerField(choices=((1,'超级管理员'),(2,'管理员'),(3,'老师'),(4,'学生')),default=4,verbose_name='用户角色')
    entrance = models.CharField(max_length=10,verbose_name='入校年月')
    stuclass = models.ForeignKey(to='stuClass',null=True,on_delete=models.CASCADE,verbose_name='班级外键')
    def __str__(self):
        return self.username
    #返回班级的班号
    @property
    def class_name(self):
        return self.stuclass.classon
    #返回性别的中文
    @property
    def sex_name(self):
        return self.get_sex_display()

#班级表
class stuClass(models.Model):
    classon = models.CharField(max_length=11,verbose_name='班级编号')
    profession = models.ForeignKey(to='Profession',on_delete=models.CASCADE,verbose_name='专业外键')
    def __str__(self):
        return self.classon

#班级与班导师表
class Class2Teacher(models.Model):
    teacher = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='班导师')
    stuclass = models.ForeignKey(to='stuClass',on_delete=models.CASCADE,verbose_name='班级')
    def __str__(self):
        return f'{self.teacher.username}:{self.stuclass.classon}'


#学生专业表
class Profession(models.Model):
    pno = models.CharField(max_length=20,verbose_name='专业编号')
    pname = models.CharField(max_length=30,verbose_name='专业名')
    college = models.ForeignKey(to='College',verbose_name='院系外键',on_delete=models.CASCADE)
    def __str__(self):
        return self.pname

#院系表
class College(models.Model):
    collegeno=models.CharField(max_length=20,verbose_name='院系编号')
    collegename =models.CharField(max_length=30,verbose_name='院系名称')
    def __str__(self):
        return self.collegename

#课程表与课程安排是一对一的关系
class Course(models.Model):
    courseno = models.CharField(max_length=20,verbose_name='课程编号')
    coursename = models.CharField(max_length=30,verbose_name='课程名称')
    studyhour = models.CharField(max_length=4,verbose_name='课时')
    credit = models.CharField(max_length=2,verbose_name='学分')
    active_time=models.CharField(max_length=12,verbose_name='上课周次：2-15',null=True)
    profession = models.ForeignKey(to='Profession',on_delete=models.CASCADE)
    semester = models.ForeignKey(to='Semester',on_delete=models.CASCADE,verbose_name='学期',null=True)
    coursetype = models.IntegerField(choices=((1,'必修'),(0,'选修')),default=1)
    @property
    def getcoursetype(self):
        return self.get_coursetype_display()

    def __str__(self):
        return self.coursename
#课程安排
class Curriculum(models.Model):
    course=models.ForeignKey(to='Course',on_delete=models.CASCADE,verbose_name='课程号')
    teacher = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='授课老师')
    semester = models.ForeignKey(to='Semester',on_delete=models.CASCADE,verbose_name='学期外键')
    stuclass = models.ForeignKey(to='stuClass',on_delete=models.CASCADE,null=True)
    teachtime = models.CharField(max_length=30,verbose_name='上课时间')
    studytime = models.IntegerField(choices=((0,'星期日'),(1,'星期1'),(2,'星期2'),(3,'星期3'),(4,'星期4'),(5,'星期5'),(6,'星期6')),verbose_name='上课星期')
    location = models.CharField(max_length=20,verbose_name='上课地点')
    #获取的是chocie中的中文
    @property
    def getstudytime(self):
        return self.get_studytime_display()


#学期表，一个学期可以有多个课程安排
class Semester(models.Model):
    semestername = models.CharField(max_length=25,verbose_name='学期名称')
    available = models.BooleanField(verbose_name='是否可用',default=True)
    create_time = models.DateField(auto_now_add=True,null=True)

#专业教学计划
class Teachplan(models.Model):
    profession=models.OneToOneField(to='Profession',on_delete=models.CASCADE,verbose_name='专业教学计划')
    plan = models.FileField(upload_to='teachplan/',verbose_name='教学计划文件')
    descn = models.TextField(verbose_name='教学计划描述')

#统考科目考试表，与科目申请表一对一关系，课程考试是要进行申请，由学校管理员审批
class Unifiedcourse(models.Model):
    course = models.ForeignKey(to='Course',on_delete=models.CASCADE,verbose_name='考试的课程')
    examtime = models.CharField(max_length=50,verbose_name='考试时间')
    available = models.BooleanField(default=False,verbose_name='是否通过')
    stuclass = models.ForeignKey(to='stuClass',on_delete=models.CASCADE,verbose_name='考试班级')
    teacher = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='申请的老师',null=True)
    location = models.CharField(max_length=50,verbose_name='考试地点',null=True)
    status = models.IntegerField(choices=((1,'未审核'),(2,'审核不通过'),(3,'审核通过')),default=1)
    relust = models.CharField(max_length=200,default='',verbose_name='审核结果')



#重考考试表
class Separateexam(models.Model):
    student = models.ManyToManyField(to='User',verbose_name='重考学生')
    course = models.ForeignKey(to='Course',on_delete=models.CASCADE,verbose_name='重考科目')
    examtime = models.CharField(max_length=30,verbose_name='考试时间')
    available = models.BooleanField(default=False,verbose_name='审批通过')
    location = models.CharField(max_length=30,verbose_name='考试地点')

#课程成绩表
class Election(models.Model):
        course = models.ForeignKey(to='Course',on_delete=models.CASCADE,verbose_name='课程名')
        student = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='学生')
        stuclass = models.ForeignKey(to='stuClass',on_delete=models.CASCADE,verbose_name='班级',null=True)
        score = models.CharField(max_length=3,verbose_name='分数',default=0)
        create_time = models.DateField(auto_now=True,null=True)
        is_pass = models.BooleanField(default=True)
        def __str__(self):
            return '%s，成绩'%self.course
        #学生名字
        @property
        def name(self):
            return self.student.name
        #学生学号
        @property
        def username(self):
            return self.student.username
        @property  #学生所在的班级
        def get_class(self):
            return self.stuclass.classon
        @property#返回课程名
        def coursename(self):
            return self.course.coursename
        @property#返回学期名
        def semester(self):
            return self.course.semester.semestername
#管理员上传的文件，批量创建用户的文件
class AdminFile(models.Model):
    filename = models.CharField(max_length=30,verbose_name='文件名')
    file = models.FileField(upload_to='admin',verbose_name='管理员文件')
    desc = models.CharField(max_length=200,verbose_name='文件描述',null=True)
    user = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='管理员')
    is_used = models.BooleanField(default=False,verbose_name='是否导入过文件')
    is_delete = models.BooleanField(default=False,verbose_name='是否删除')

#获奖表
class Prize(models.Model):
    college = models.ForeignKey(to='College',on_delete=models.CASCADE,verbose_name='学院')
    profession = models.ForeignKey(to='Profession',on_delete=models.CASCADE,verbose_name="专业")
    student = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='学生')
    allcourse = models.IntegerField(verbose_name='课程总分数',default=0)
    level = models.IntegerField(choices=((1,'学院一等奖'),(2,'学院二等奖'),(3,'学院三等奖')))
    stuclass = models.ForeignKey(to='stuClass',on_delete=models.CASCADE,verbose_name='班级')
    semester = models.ForeignKey(to='Semester',on_delete=models.CASCADE,verbose_name='学期')
    class Meta:
        #设置联合唯一
        unique_together = ['student', 'college','semester']
        #
    def __str__(self):
        #打印对象，是学生的学号
        return self.student.username
    @property
    def classname(self):
        return self.stuclass.classon
    @property
    def semestername(self):
        return self.semester.semestername
    @property
    def collegename(self):
        return self.college.collegename
    @property
    def professionname(self):
        return self.profession.pname
    @property
    def username(self):
        return self.student.username

#管理员上传的奖学金excel表
class PrizeFile(models.Model):
    name = models.CharField(max_length=50,verbose_name='文件名字')
    desc = models.CharField(max_length=50,verbose_name='文件描述')
    user = models.ForeignKey(to='User',on_delete=models.CASCADE)
    file = models.FileField(upload_to='prize',verbose_name='文件上传的位置')
    is_used = models.BooleanField(default=False)
    is_deleted = models.BooleanField(default=False)


#老师与专业表
class TeaProfession(models.Model):
    teacher = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='老师')
    profession = models.ForeignKey(to='Profession',on_delete=models.CASCADE,verbose_name='专业')
    def __str__(self):
        return f'{self.teacher.username},{self.profession.pname}'
#考试申请失败的,不会使用到的
class ExamFaile(models.Model):
    unifile = models.ForeignKey(to='Unifiedcourse',on_delete=models.CASCADE,verbose_name='考试申请')
    why = models.TextField(verbose_name='审批失败的原因')

#公告的表
class SchoolMessege(models.Model):
    title = models.CharField(max_length=50,verbose_name='公告的标题')
    context = models.TextField(verbose_name='公告内容')

#选修课表
class ChoiceCourse(models.Model):
    courseid = models.CharField(max_length=11,verbose_name='选修课id')
    coursename = models.CharField(max_length=20,verbose_name='课程名字')
    semster = models.ForeignKey(to='Semester', on_delete=models.CASCADE, verbose_name='上课学期')
    credit = models.SmallIntegerField(verbose_name='学分', default=3)
    studytime = models.IntegerField(verbose_name='学时', default=48)
    students = models.IntegerField(verbose_name='上课人数')

    is_pass = models.IntegerField(choices=((1,'未审批'),(2,'审批通过'),(3,'审批不通过')),default=1)
    why = models.CharField(max_length=200,null=True)
    new_count = models.IntegerField(verbose_name='已选人数',default=0)
    teacher = models.ForeignKey(to='User', on_delete=models.CASCADE, verbose_name='授课老师',null=True,db_constraint=False)
    college = models.ForeignKey(to='College', on_delete=models.CASCADE, verbose_name='学院',null=True,db_constraint=False)
    can_choice = models.BooleanField(default=True,verbose_name='标记该课程是否还有名额')
    is_show=models.BooleanField(default=False,verbose_name='标志学生是否可以开始进行选课')
    @property
    def getis_pass(self):
        return self.get_is_pass_display()
    @property
    def get_collegename(self):
        return self.college.collegename

#选修课记录表
class StudentChoiceCourse(models.Model):
    course = models.ForeignKey(to='ChoiceCourse',on_delete=models.CASCADE,verbose_name='选课')
    student = models.ForeignKey(to='User',on_delete=models.CASCADE,verbose_name='学生')
    create_time=models.DateTimeField(auto_now_add=True,verbose_name='选课时间')

#选修课上课表
class ChoiceCourseStudy(models.Model):
    location = models.CharField(max_length=20,verbose_name='上课地点')
    weeks = models.CharField(max_length=10,verbose_name='上课周次')
    section = models.CharField(max_length=20,verbose_name='上课节次')
    course = models.ForeignKey(to='ChoiceCourse',on_delete=models.CASCADE,verbose_name='课程')
    days = models.IntegerField(choices=((0,'星期日'),(1,'星期1'),(2,'星期2'),(3,'星期3'),(4,'星期4'),(5,'星期5'),(6,'星期6')),verbose_name='上课星期')
    @property
    def get_days(self):
        return self.get_days_display()
import os
from  django.conf import settings
#老师、管理员生成学生信息的excel表格
class StudentMessageExcel(models.Model):
    user = models.ForeignKey(to='User',on_delete=models.CASCADE)
    filename = models.CharField(max_length=100,verbose_name='文件名')
    create_time = models.DateTimeField(auto_now=True)
    @property
    def username(self):
        return self.user.username
    @property
    def file_path(self):
        #返回文件路径
        return os.path.join('http://127.0.0.1:8000/','media','students_excels',self.filename)
#老师、管理员生成学生成绩的excel表格

class StudentCreditExcel(models.Model):
    user = models.ForeignKey(to='User',on_delete=models.CASCADE)
    filename = models.CharField(max_length=100,verbose_name='文件名')
    create_time = models.DateField(auto_now=True)
    @property
    def username(self):
        return self.user.username
    @property
    def file_path(self):
        return os.path.join('http://127.0.0.1:8000/','media','credit_excels',self.filename)

#一个学期一个专业的所有学生的课程成绩excel表格
class ProfessionCreditExcel(models.Model):
    user = models.ForeignKey(to='User',on_delete=models.CASCADE)
    filename = models.CharField(max_length=100,verbose_name='文件名')
    create_time = models.DateField(auto_now=True)
    @property
    def username(self):
        return self.user.username
    @property
    def file_path(self):
        return os.path.join('http://127.0.0.1:8000/','media','profession_cred_excel',self.filename)